package com.fourth_groups.shopping121store.ui.home.view;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.ui.home.model.Home;
import com.fourth_groups.shopping121store.ui.home.model.HomeFoodBean;
import com.fourth_groups.shopping121store.ui.home.presenter.Presenter;

import java.util.List;


/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃  创建人:李培雄
 * 　　┃　┳┛　┗┳　┃  时间:Administrator on 2016/10/10
 * 　　┃　　　　　　　┃  备注:首页
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━by:coder-pig
 */
public class HomeFragment extends Fragment implements IHomeView, View.OnClickListener {

    public static final int SUCCESS = 1;
    private static HomeFragment homeFragment;
    private Toolbar mBar;
    private ViewPager mVp;
    private TabLayout mTab;
    private HomeVpAdapter homeVpAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static Fragment setFragment() {
        if (homeFragment == null)
            synchronized (HomeFragment.class) {
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
            }
        return homeFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBar = (Toolbar) view.findViewById(R.id.home_toolbar);
        mTab = (TabLayout) view.findViewById(R.id.home_tab);
        mVp = (ViewPager) view.findViewById(R.id.home_viewpager);
        view.findViewById(R.id.home_qd).setOnClickListener(this);
        Presenter presenter = new Presenter(this, null);
        presenter.getNavigationData(0);
    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }

    @Override
    public ViewPager getViewPager() {
        return null;
    }

    @Override
    public void getHomeNavigationData(final Home home) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    List<Home.DataBean.BlockBean.ItemsBean> items = home.getData().getBlock().get(0).getItems();
                    //设置TabLayout模式
                    mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    homeVpAdapter = new HomeVpAdapter(getFragmentManager(), items);
                    mVp.setAdapter(homeVpAdapter);
                    mVp.setOffscreenPageLimit(items.size());
                    //关联ViewPager
                    mTab.setupWithViewPager(mVp);
                }
            });
        }
    }

    @Override
    public void getFoodData(HomeFoodBean home) {
    }

    @Override
    public void getFail() {

    }

    @Override
    public Context getContexts() {
        return getActivity().getApplicationContext();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_qd:
                Toast.makeText(getActivity(), "签到成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
